﻿Imports TyranntEngine

Public Class NewPasswordForm

    Private WithEvents _client As New TyranntOnlineUserService.TyranntUserServiceClient
    Private _token As String
    Private _userID As Integer

    Public WriteOnly Property Token() As String
        Set(ByVal value As String)
            _token = value
        End Set
    End Property

    Public WriteOnly Property UserID() As Integer
        Set(ByVal value As Integer)
            _userID = value
        End Set
    End Property

    Public Sub CheckForm()
        okButton.Enabled = False
        If passwordTextBox.Text <> "" And RepeatPasswordTextBox.Text <> "" Then
            If passwordTextBox.Text <> RepeatPasswordTextBox.Text Then
                passwordTextBox.BackColor = Color.LightSalmon
                RepeatPasswordTextBox.BackColor = Color.LightSalmon
            Else
                passwordTextBox.BackColor = Color.LightGreen
                RepeatPasswordTextBox.BackColor = Color.LightGreen
                okButton.Enabled = True
            End If
        Else
            passwordTextBox.BackColor = Color.White
            RepeatPasswordTextBox.BackColor = Color.White
        End If
    End Sub

    Private Sub passwordTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles passwordTextBox.TextChanged
        CheckForm()
    End Sub

    Private Sub RepeatPasswordTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RepeatPasswordTextBox.TextChanged
        CheckForm()
    End Sub

    Private Sub NewPasswordForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckForm()
    End Sub

    Private Sub cancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles canceledButton.Click
        Me.Close()
    End Sub

    Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
        _client.ChangePasswordAsync(_token, _userID, EncryptPassword(passwordTextBox.Text))
    End Sub

    Private Sub _client_ChangePasswordCompleted(ByVal sender As Object, ByVal e As TyranntOnlineUserService.ChangePasswordCompletedEventArgs) Handles _client.ChangePasswordCompleted
        If e.Result <> "" Then
            MessageBox.Show(e.Result, "Change Password Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        Me.Close()
    End Sub
End Class